/**
 * 数字信封加解密
 * @param {object} serviceApi - 接口API
 */

import JSEncrypt from 'jsencrypt'
import CryptoJS from 'crypto-js'

// 默认的 KEY 与 iv 如果没有给
const KEY = CryptoJS.enc.Utf8.parse("caiquan1234567890")
const IV = CryptoJS.enc.Utf8.parse('E08YUI26998YHB87')
// RSA 加密公钥（app端的公钥）
let RSA_PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2OrF2iXr23Iz9VbQOQnxAir/J3ukZe61kv4AGZ/HDACElIENejYU2ncnXtJJR/5gmBoJZfu9zrpv1dhNRCFfZt0T74ECuPGcMEQhxmkm4qK/ZoNiAWI7YJpDGk4zj4ZI9htZzz29kBssKBXOcvqKsYZUkTIhByODaY+S/wwOdpwIDAQAB';
  // var RSA_PUBLIC_KEY = '';

let RSA_PRIVATE_KEY = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALY6sXaJevbcjP1VtA5CfECKv8ne6Rl7rWS/gAZn8cMAISUgQ16NhTadyde0klH/mCYGgll+73Oum/V2E1EIV9m3RPvgQK48ZwwRCHGaSbior9mg2IBYjtgmkMaTjOPhkj2G1nPPb2QGywoFc5y+oqxhlSRMiEHI4Npj5L/DA52nAgMBAAECgYBeh9H5aNYV61FkNapC33BvVsqo4O7Q3lyTb9fs+szqWE3ZYYQxL37UV0QWlsB0sv9tcmD6JiP3QbP7wHXCym4vTSWsNscQrZwS4KkIPhFpEDSehOQ9jO541OmhFY9g6KqEue7w5f/8c+n/KV11qiZZXilHZU4dPR29MzRVjz+9sQJBAOWiDzOahK6zxPWuK62JHpqfy1otq4UGw+2PIDkhukyjyTMMmflI+mrUf/H9tRMvg3QWwAATt80Orz6jbzYxhIkCQQDLJzmolKqhBjuiDSLCM9dQo3WGTFsOXxRa9q+g+r4NS+g/2fGnk9BIVpHhyZn+Df82+34GR1MHBqDRhjGsIOSvAkBieeo8/hHSfeOEXIn8pKYMN2TUqms45BrX4J44Af/L6B1bmBbb8fD6KWEpHN0vehvMiAkUv1/+P0LmAN6rhfbJAkEAr+fgEivgbxuBiTkMjtHqr4VKHyaAdbo4Wuddfet/ou3GRXuGto7T+78SBErXT1d5VHeBURhzORMxLxueaSf2OwJBAJRue4tf2DTsyHuA9pDiNJsyIgoWwOzNjMGZOyaavisqYCq6jscOUYkU5Qbhg6pgHn7vj3sJ6hUojZw9m9GYHRk=';

let aesKey = '';

// 获取公钥
let getPublicKey = function(params){
  let url ='https://haikou.eshiyun.info/attendance-manager/getPublicKey';
  let xmlhttp = new XMLHttpRequest();
  xmlhttp.timeout = 0; // 设置超时时间,0表示永不超时
  xmlhttp.open("post", url, false);
  xmlhttp.send(null);
  return xmlhttp.responseText
};


// aes加密key
const getAESKey = function(){
  var key = "";
  var str = "ycsmy";
  var str2 = ( new Date ).getTime();
  var str3 = str + str2;
  var str4 = CryptoJS.MD5(str3).toString();
  key = str4.substr(0, 16);
  aesKey = key;
  return key;
}

getAESKey();

// rsa加密
const RSAEncrypt = function (word) {
  // if(!RSA_PUBLIC_KEY) {
  //   const data = JSON.parse(getPublicKey());
  //   if(data.code == 200) {
  //     RSA_PUBLIC_KEY = data.data;
  //   }
  // }
  let obj = new JSEncrypt();
  obj.setPublicKey(RSA_PUBLIC_KEY);
  let data = obj.encrypt(word);
  return data;
}

// rsa解密
const RSADecrypt = function (word) {
  let obj = new JSEncrypt();
  obj.setPrivateKey(RSA_PRIVATE_KEY);
  let data = obj.decrypt(word);
  return data;
}

// aes加密
const AESEncrypt = function(word, keyStr=aesKey) {
  let key = KEY
  let iv = IV
  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr);
  }
  var encrypted = CryptoJS.AES.encrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  encrypted = encrypted.toString();
  return encrypted;
}

// aes解密
const AESDecrypt = function(word, keyStr=aesKey) {
  let key  = KEY;
  let iv = IV;
  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr);
  }
  let base64 = CryptoJS.enc.Base64.parse(word);
  let src = CryptoJS.enc.Base64.stringify(base64);

  var decrypt = CryptoJS.AES.decrypt(src, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });

  var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}

// let handler = {
//   get: function(object, prop) {
//     switch(prop) {
//       case 'RSAEncrypt':
//         return RSAEncrypt(object.word)
//         break
//       case 'RSADecrypt':
//         return RSADecrypt(object.word)
//         break
//       case 'AESEncrypt':
//         return AESEncrypt(object.word)
//         break
//       case 'AES_RSA_Encrypt':
//         const data = AESEncrypt(object.word);
//         const rasKey = RSAEncrypt(aesKey);
//         return {data: data, key: rasKey}
//         break
//       case 'AESDecrypt':
//         return AESDecrypt(object.word)
//         break
//       default: 
//         return '请输入正确的方法'
//     }
//   }
// }
// let proxy = new Proxy({}, handler);

// const Encryption = function(data) {
//   proxy.word = data.word;
//   return proxy[data.type]
// }

const Encryption = function(e) {
  const name = e.type;
  const word = e.word || '';
  if(name == 'AES_RSA_Encrypt') {
    const data = AESEncrypt(word);
    const rasKey = RSAEncrypt(aesKey);
    return {data: data, key: rasKey}

  }else if(name == 'AESDecrypt') {
    return AESDecrypt(word)

  }else if(name == 'RSAEncrypt') {
    return RSAEncrypt(word)

  }else if(name == 'RSADecrypt') {
    return RSADecrypt(word)

  }else {
    return '请输入正确的方法'
  }

}

export default Encryption